/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddTransactionPurchase.java
 *
 * Created on Nov 6, 2011, 4:43:12 AM
 */
package mpinventory;

import java.sql.*;
import com.michaelbaranov.microba.calendar.DatePicker;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.ArrayList;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.DefaultListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
 *
 * @author msi
 */
public class AddTransactionPurchase extends javax.swing.JPanel {

    private Connection conn;
    private Dimension size;
    private DateFormat dateFormat=new SimpleDateFormat("MMMM dd, yyyy");
    private Calendar calendar=Calendar.getInstance();
    private Date date=new Date(calendar.get(calendar.YEAR)-1900,calendar.get(calendar.MONTH),calendar.get(calendar.DAY_OF_MONTH));
    public DatePicker datePicker=new DatePicker(date,dateFormat);
    private TransactionPurchaseQuery purchaseQuery;
    private List<Item> items; //list of all items
    private List<Supplier> suppliers; //list of all suppliers
    private List<Integer> itemIDs;
    private List<String> descriptions;
    private List<Float> quantities;
    private List<Float> prices; //list of items in table
    private DefaultTableModel model;
    public int width;
    public int height;
    public int screenWidth;
    public int screenHeight;
    private int selectedItemIndex=-1;
    private int selectedSupplierIndex=-1;
    private int nRows=0;
    public boolean backToMainMenu=false;
    private String data[][] = {};
    private String col[] = {"Item", "Quantity", "Unit", "Price"};
    /** Creates new form AddTransactionPurchase */
    public AddTransactionPurchase() {
        initComponents();
    }
    public AddTransactionPurchase(Connection conn, int screenWidth, int screenHeight) {
        this.conn=conn;
        this.screenWidth=screenWidth;
        this.screenHeight=screenHeight;
        myInitComponents();
        size=this.getPreferredSize();
        width=size.width+10;
        height=size.height+35;
        purchaseQuery=new TransactionPurchaseQuery(conn);
        model = new DefaultTableModel(data, col){
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Float.class, java.lang.String.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, true
            };
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        };
        tableTPACompanyOrders.setModel(model);
        tableTPACompanyOrders.getColumnModel().getColumn(0).setResizable(false);
        tableTPACompanyOrders.getColumnModel().getColumn(0).setPreferredWidth(250);
        tableTPACompanyOrders.getColumnModel().getColumn(1).setResizable(false);
        tableTPACompanyOrders.getColumnModel().getColumn(1).setPreferredWidth(10);
        tableTPACompanyOrders.getColumnModel().getColumn(2).setResizable(false);
        tableTPACompanyOrders.getColumnModel().getColumn(2).setPreferredWidth(10);
        tableTPACompanyOrders.getColumnModel().getColumn(3).setResizable(false);
        tableTPACompanyOrders.getColumnModel().getColumn(3).setPreferredWidth(60);
        initializeItemList();
        initializeSupplierList();
        itemIDs=new ArrayList<Integer>();
        txtTPAPurchaseID.setText(purchaseQuery.setID()+"");
    }
    @Override
    public int getWidth(){
        return width;
    }
    @Override
    public int getHeight(){
        return height;
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtTPAPurchaseID = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cbxTPAStatus = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listTPAItems = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableTPACompanyOrders = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        btnTPAAdd = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        btnTPARemove = new javax.swing.JButton();
        btnTPASave = new javax.swing.JButton();
        btnTPACancel = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        listTPASuppliers = new javax.swing.JList();

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 25));
        jLabel1.setText("Add Company Purchase");

        jLabel2.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel2.setText("Purchase ID:");

        jLabel3.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel3.setText("Supplier:");

        jLabel5.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel5.setText("Date:");

        txtTPAPurchaseID.setEditable(false);
        txtTPAPurchaseID.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        jLabel4.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel4.setText("Status:");

        cbxTPAStatus.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        cbxTPAStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pending", "Delivered" }));
        cbxTPAStatus.setEnabled(false);

        jLabel6.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel6.setText("List of all items:");

        jScrollPane1.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        listTPAItems.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        listTPAItems.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Copper Tube S.D. 1/4\" x .022mm x 50'", "Copper Tube S.D. 3/8\" x .028mm x 50'", "Copper Tube S.D. 1/2\" x .028mm x 50'", "Copper Tube S.D. 5/8\" x .028mm x 50'", "Copper Tube S.D. 3/4\" x .028mm x 50'" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(listTPAItems);

        tableTPACompanyOrders.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N
        tableTPACompanyOrders.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Item", "Quantity", "Unit", "Price"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Float.class, java.lang.String.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableTPACompanyOrders.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(tableTPACompanyOrders);
        tableTPACompanyOrders.getColumnModel().getColumn(0).setResizable(false);
        tableTPACompanyOrders.getColumnModel().getColumn(0).setPreferredWidth(250);
        tableTPACompanyOrders.getColumnModel().getColumn(1).setResizable(false);
        tableTPACompanyOrders.getColumnModel().getColumn(1).setPreferredWidth(10);
        tableTPACompanyOrders.getColumnModel().getColumn(2).setResizable(false);
        tableTPACompanyOrders.getColumnModel().getColumn(2).setPreferredWidth(10);
        tableTPACompanyOrders.getColumnModel().getColumn(3).setResizable(false);
        tableTPACompanyOrders.getColumnModel().getColumn(3).setPreferredWidth(60);

        jLabel7.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel7.setText("Company Orders:");

        jLabel10.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel10.setText("Add to -------------->");

        btnTPAAdd.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTPAAdd.setText("Add");
        btnTPAAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemActionPerformed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel9.setText("Remove from ----->");

        btnTPARemove.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTPARemove.setText("Remove");
        btnTPARemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeItemActionPerformed(evt);
            }
        });

        btnTPASave.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTPASave.setText("Save");
        btnTPASave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        btnTPACancel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTPACancel.setText("Cancel");
        btnTPACancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        listTPASuppliers.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        listTPASuppliers.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "New Ever Lumber And Hardware", "To Suy", "3 Gold Marketing Corporation" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(listTPASuppliers);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(288, 288, 288)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(txtTPAPurchaseID, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel3)
                        .addGap(50, 50, 50)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 339, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(10, 10, 10)
                        .addComponent(cbxTPAStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel6)
                        .addGap(286, 286, 286)
                        .addComponent(jLabel7))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnTPASave, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(4, 4, 4)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(1, 1, 1)
                                        .addComponent(jLabel10))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(29, 29, 29)
                                        .addComponent(btnTPAAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel9)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(29, 29, 29)
                                        .addComponent(btnTPARemove)))))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 511, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(88, 88, 88)
                                .addComponent(btnTPACancel, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel1)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel2))
                    .addComponent(txtTPAPurchaseID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel4)
                        .addGap(21, 21, 21)
                        .addComponent(jLabel5))
                    .addComponent(cbxTPAStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addGap(18, 18, 18)
                        .addComponent(btnTPAAdd)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel9)
                        .addGap(18, 18, 18)
                        .addComponent(btnTPARemove))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnTPACancel)
                    .addComponent(btnTPASave))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
private void myInitComponents(){
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtTPAPurchaseID = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cbxTPAStatus = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listTPAItems = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableTPACompanyOrders = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        btnTPAAdd = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        btnTPARemove = new javax.swing.JButton();
        btnTPASave = new javax.swing.JButton();
        btnTPACancel = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        listTPASuppliers = new javax.swing.JList();

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 25)); // NOI18N
        jLabel1.setText("Add Company Purchase");

        jLabel2.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel2.setText("Purchase ID:");

        jLabel3.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        jLabel3.setText("Supplier:");

        jLabel5.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        jLabel5.setText("Date:");

        txtTPAPurchaseID.setEditable(false);
        txtTPAPurchaseID.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        jLabel4.setText("Status:");

        cbxTPAStatus.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N
        cbxTPAStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pending", "Delivered" }));

        jLabel6.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        jLabel6.setText("List of all items:");

        jScrollPane1.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        listTPAItems.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N
        listTPAItems.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Copper Tube S.D. 1/4\" x .022mm x 50'", "Copper Tube S.D. 3/8\" x .028mm x 50'", "Copper Tube S.D. 1/2\" x .028mm x 50'", "Copper Tube S.D. 5/8\" x .028mm x 50'", "Copper Tube S.D. 3/4\" x .028mm x 50'" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(listTPAItems);

        tableTPACompanyOrders.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N
        tableTPACompanyOrders.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Item", "Quantity", "Unit", "Price"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Float.class, java.lang.String.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableTPACompanyOrders.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(tableTPACompanyOrders);
        tableTPACompanyOrders.getColumnModel().getColumn(0).setResizable(false);
        tableTPACompanyOrders.getColumnModel().getColumn(0).setPreferredWidth(250);
        tableTPACompanyOrders.getColumnModel().getColumn(1).setResizable(false);
        tableTPACompanyOrders.getColumnModel().getColumn(1).setPreferredWidth(10);
        tableTPACompanyOrders.getColumnModel().getColumn(2).setResizable(false);
        tableTPACompanyOrders.getColumnModel().getColumn(2).setPreferredWidth(10);
        tableTPACompanyOrders.getColumnModel().getColumn(3).setResizable(false);
        tableTPACompanyOrders.getColumnModel().getColumn(3).setPreferredWidth(60);

        jLabel7.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        jLabel7.setText("Company Orders:");

        jLabel10.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        jLabel10.setText("Add to -------------->");

        btnTPAAdd.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        btnTPAAdd.setText("Add");
        btnTPAAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemActionPerformed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        jLabel9.setText("Remove from ----->");

        btnTPARemove.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        btnTPARemove.setText("Remove");
        btnTPARemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeItemActionPerformed(evt);
            }
        });

        btnTPASave.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        btnTPASave.setText("Save");
        btnTPASave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        btnTPACancel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        btnTPACancel.setText("Cancel");
        btnTPACancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        listTPASuppliers.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N
        listTPASuppliers.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "New Ever Lumber And Hardware", "To Suy", "3 Gold Marketing Corporation" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(listTPASuppliers);

        datePicker.setShowNumberOfWeek(true);
        datePicker.setShowNoneButton(false);
        datePicker.setFieldEditable(false);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(288, 288, 288)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(txtTPAPurchaseID, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel3)
                        .addGap(50, 50, 50)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 339, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(datePicker, datePicker.preferredSize().width,datePicker.preferredSize().width,datePicker.preferredSize().width)
                            .addComponent(cbxTPAStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel6)
                        .addGap(286, 286, 286)
                        .addComponent(jLabel7))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnTPASave, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(4, 4, 4)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(1, 1, 1)
                                        .addComponent(jLabel10))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(29, 29, 29)
                                        .addComponent(btnTPAAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel9)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(29, 29, 29)
                                        .addComponent(btnTPARemove)))))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 511, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(88, 88, 88)
                                .addComponent(btnTPACancel, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel1)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel2))
                    .addComponent(txtTPAPurchaseID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel4)
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(datePicker,datePicker.preferredSize().height,datePicker.preferredSize().height,datePicker.preferredSize().height)))
                    .addComponent(cbxTPAStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addGap(18, 18, 18)
                        .addComponent(btnTPAAdd)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel9)
                        .addGap(18, 18, 18)
                        .addComponent(btnTPARemove))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnTPACancel)
                    .addComponent(btnTPASave))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
}
    private void initializeItemList(){
        ItemQuery itemQuery=new ItemQuery(conn);
        items=itemQuery.initializeItems();
        DefaultListModel listModel = new DefaultListModel();
        for(int i=0;i<items.size();i++)
            listModel.addElement(items.get(i).getDescription());
        listTPAItems.setModel(listModel);
        selectedItemIndex=0;
        listTPAItems.addListSelectionListener(new ListSelectionListener(){
            @Override
            public void valueChanged(ListSelectionEvent e){
                try{
                    selectedItemIndex=listTPAItems.getSelectedIndex();
                }catch(Exception f){}
            }
        });
    } //end of initializeItemList() method
    private void initializeSupplierList(){
        SupplierQuery supplierQuery=new SupplierQuery(conn);
        suppliers=supplierQuery.initializeSuppliers();
        DefaultListModel listModel = new DefaultListModel();
        for(int i=0;i<suppliers.size();i++)
            listModel.addElement(suppliers.get(i).getName());
        listTPASuppliers.setModel(listModel);
        selectedSupplierIndex=0;
        listTPASuppliers.addListSelectionListener(new ListSelectionListener(){
            @Override
            public void valueChanged(ListSelectionEvent e){
                try{
                    selectedSupplierIndex=listTPASuppliers.getSelectedIndex();
                }catch(Exception f){}
            }
        });
    } //end of initializeSupplierList() method
    private boolean validTable(){
        for(int i=0;i<model.getRowCount();i++)
            for(int j=0;j<model.getColumnCount();j++)
                try{
                    if(model.getValueAt(i,j).equals(null))
                        return false;
                }catch(NullPointerException e){
                    return false;
                }
        return true;
    } //end of validTable() method
    private boolean tableIsEmpty(){
        for(int i=0;i<model.getRowCount();i++)
            for(int j=0;j<model.getColumnCount();j++)
                try{
                    if(!model.getValueAt(i,j).toString().isEmpty())
                        return false;
                }catch(NullPointerException e){
                    return false;
                }
        return true;
    } //end of tableIsEmpty() method
    private void getTableValues(){
        descriptions=new ArrayList<String>();
        quantities=new ArrayList<Float>();
        prices=new ArrayList<Float>();
        String[] rowData=new String[model.getColumnCount()];
        for(int i=0;i<model.getRowCount();i++){
            for(int j=0;j<model.getColumnCount();j++)
                rowData[j]=model.getValueAt(i, j).toString();
            descriptions.add(rowData[0]);
            quantities.add(Float.parseFloat(rowData[1]));
            prices.add(Float.parseFloat(rowData[2]));
        }
    }
    private int getDayDiff(int year1, int month1, int day1, int year2, int month2, int day2){
        int dayDiff;
        int monthDiff;
        int yearDiff;
        if(day1<day2){
            day1+=30;
            month1-=1;
        }
        dayDiff=day1-day2;
        if(month1<month2){
            month1+=12;
            year1-=1;
        }
        monthDiff=month1-month2;
        yearDiff=year1-year2;
        dayDiff=yearDiff*12*30+monthDiff*30+dayDiff;
        return dayDiff;
    }

private void addItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addItemActionPerformed
        Object[] rowData=new Object[4];
        rowData[0]=items.get(selectedItemIndex).getDescription();
        rowData[2]=items.get(selectedItemIndex).getQuantityUnit();
        model.insertRow(nRows,rowData);
        nRows++;
        tableTPACompanyOrders.setModel(model);
        itemIDs.add(items.get(selectedItemIndex).getID());
}//GEN-LAST:event_addItemActionPerformed

private void removeItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeItemActionPerformed
        if(tableTPACompanyOrders.isEditing())
            JOptionPane.showMessageDialog(null, "Can't remove while editing a cell!");
        else if(tableTPACompanyOrders.getSelectedRow() == -1)
            JOptionPane.showMessageDialog(null, "Nothing selected!");
        else{
            itemIDs.remove(tableTPACompanyOrders.getSelectedRow());
            model.removeRow(tableTPACompanyOrders.getSelectedRow());
            nRows--;
        }
        tableTPACompanyOrders.setModel(model);
}//GEN-LAST:event_removeItemActionPerformed

private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        purchaseQuery=new TransactionPurchaseQuery(conn);
        if(tableTPACompanyOrders.isEditing())
            JOptionPane.showMessageDialog(null, "Can't save while editing a cell!");
        else if(!validTable() || tableIsEmpty())
            JOptionPane.showMessageDialog(null, "All fields cannot be empty!");
        else if(datePicker.getDate().after(date))
            JOptionPane.showMessageDialog(null, "Stay on the current time!");
        else if(getDayDiff(date.getYear(),date.getMonth(),date.getDate(),datePicker.getDate().getYear(),datePicker.getDate().getMonth(),datePicker.getDate().getDate())>180)
            JOptionPane.showMessageDialog(null, "Stay on the current time!");
        else{
            JOptionPane pane=new JOptionPane("Are you sure you want to add this purchase?");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Confirm Purchase Addition");
            dialog.show();
            if(options[0].equals(pane.getValue())){
                getTableValues();
                purchaseQuery.addPurchase(purchaseQuery.setID(),suppliers.get(selectedSupplierIndex).getID(),suppliers.get(selectedSupplierIndex).getName(),datePicker.getDate(),cbxTPAStatus.getSelectedItem().toString(),quantities,itemIDs,descriptions,prices);
                backToMainMenu=true;
            } //YES
        }
}//GEN-LAST:event_saveActionPerformed

private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        if(tableIsEmpty())
            backToMainMenu=true;
        else{
            JOptionPane pane=new JOptionPane("Are you sure you want to go back to the main menu? Please make sure that all changes have been saved.");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Back To Main Menu?");
            dialog.show();
            if(options[0].equals(pane.getValue())) //YES
                backToMainMenu=true;
        }
}//GEN-LAST:event_cancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnTPAAdd;
    private javax.swing.JButton btnTPACancel;
    private javax.swing.JButton btnTPARemove;
    private javax.swing.JButton btnTPASave;
    private javax.swing.JComboBox cbxTPAStatus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList listTPAItems;
    private javax.swing.JList listTPASuppliers;
    private javax.swing.JTable tableTPACompanyOrders;
    private javax.swing.JTextField txtTPAPurchaseID;
    // End of variables declaration//GEN-END:variables
}
